var FeedMaterialDetailModel = function (material, feed, mode) {
  BaseModel.call(this);

  var self = this;

  var warehouseList = {
    "1": "原材料仓",
    "2": "辅料仓"
  };
  var changeFields = ['materialName', 'model', 'color', 'providerName'];
  var requiredFields = ['materialName', 'model', 'providerName'];
  var feedFields = ['materialType', 'unit', 'unitPrice', 'procurementCycles'];
  var fn = {
    initData: function (material, feed, mode) {
      switch (mode) {
        case 'feed':
          self.mode = 'feed';
          this.initFeedData(material, feed);
          break;
        case 'selectFeed':
          this.initSelectFeedData(material);
          this.setMaterialDept(material);
          break;
        case 'orderFeed':
          self.mode = 'nonFeed';
          this.initOrderFeedData(feed);
          break;
        case 'nonFeed':
          self.mode = 'nonFeed';
          this.initNonFeedData(material, feed);
          this.setMaterialDept(material);
          break;
        case 'prePurchaseFeed':
          self.mode = 'nonFeed';
          this.initPrePurchaseFeedData(material, feed);
          break;
      }
    },
    initFeedData: function (material, feed) {
      this.initOrderData(material);
      this.initColor(material, feed);
      self.setDataFromObj(feed, ['feedCount', 'detailId', 'materialType', 'haveYltsTotal']);
      self.saveOldData();
    },
    initSelectFeedData: function (material) {
      this.initOrderData(material);
      self.setDataFromObj(material, ['materialType', 'colorDescription'])
    },
    initNonFeedData: function (material, feed) {
      this.initOrderData(material, feed);
      this.initColor(material, feed);
      self.setDataFromObj(feed, ['company', 'orderNo', 'materialType', 'haveYltsTotal']);
      self.saveOldData();

      if (feed.getFeedMain !== undefined) {
        self.oldFeed = feed.getFeedMain();
      } else {
        self.oldFeed = feed.oldFeed;
      }
    },
    initOrderFeedData: function (feed) {
      var fields = ['materialName', 'color', 'colorDescription', 'providerName', 'materialType', 'material', 'unit', 'isEquals', 'materialCode',
        'company', 'orderNo', 'haveYltsTotal', 'materialDept'
      ];
      self.setDataFromObj(feed, fields, '', true);

      var model = feed.materialModel;
      if (SysUtil.isEmpty(model)) {
        model = '';
      }
      self.model = model.trim();

      self.transmittance = undefined;
      self.unitPrice = undefined;
      self.procurementCycles = undefined;
      self.id = feed.id;

      var materialFactory = feed.materialFactory;
      var defaultWarehouse = warehouseList[materialFactory];
      if (defaultWarehouse === undefined) {
        defaultWarehouse = '原材料仓';
      }
      self.defaultWarehouse = defaultWarehouse;

      if (self.unit === '付') {
        self.isEquals = 1;
      } else {
        self.isEquals = 0;
      }

      self.saveOldData();
      self.oldFeed = feed.getFeedMain();
    },
    initOrderData: function (material) {
      var fields = ['color', 'defaultWarehouse', 'material', 'unit',
        'unitPrice', 'procurementCycles', 'transmittance', 'materialCode'
      ];
      fields.pushArray(changeFields);
      self.setDataFromObj(material, fields, '', true);

      if (self.transmittance === '') {
        self.transmittance = undefined;
      }

      if (self.unit === '付') {
        self.isEquals = 1;
      } else {
        self.isEquals = 0;
      }
    },
    initPrePurchaseFeedData: function (detail, feed) {
      var fields = ['colorDescription', 'materialType', 'unit', 'material',
        'defaultWarehouse', 'procurementCycles', 'transmittance'
      ];
      fields.pushArray(changeFields);
      self.setDataFromObj(detail, fields, '', true);

      var feedFields = ['isEquals', 'materialCode', 'company', 'orderNo', 'haveYltsTotal'];
      self.setDataFromObj(feed, feedFields);

      if (self.unit === '付') {
        self.isEquals = 1;
      } else {
        self.isEquals = 0;
      }
      self.unitPrice = detail.price;
      self.saveOldData();
      self.oldFeed = feed.getFeedMain();
    },
    initColor: function (material, feed) {
      self.colorDescription = feed.colorDescription
    },
    getErrorMsg: function (code) {
      var errorObj = {
        materialName: "请输入物料名称",
        model: "请输入规格型号",
        providerName: "请选择供应商名称",
        color: "请输入编号",
        feedCount: "请输入投料数",
        materialType: "请选择物料类型",
        unit: "请选择单位",
        unitPrice: "请输入单价",
        procurementCycles: "请输入采购周期"
      };
      return errorObj[code];
    },
    setMaterialDept: function (material) {
      var materialDeptId = material.materialDeptId;
      var materialDeptName = material.materialDeptName;
      var materialDept;
      if (SysUtil.isEmpty(materialDeptId) || SysUtil.isEmpty(materialDeptName)) {
        materialDept = '';
      } else {
        materialDept = {
          materialDeptId: materialDeptId,
          materialDeptName: materialDeptName
        };
      }
      self.materialDept = materialDept;
      self.materialDeptId = materialDeptId;
      self.materialDeptName = materialDeptName;
    }
  };

  this.saveOldData = function () {
    this.setOldData(changeFields);
  };

  this.isChange = function () {
    for (var i = 0; i < changeFields.length; i++) {
      var field = changeFields[i];
      var oldValue = this.getOldData(field);
      var value = _.get(this, field);
      if (oldValue !== value) {
        return true;
      }
    }
    return false;
  };

  this.getSearchObject = function () {
    var postData = {};
    this.setPostData(postData, changeFields, true, true);
    if (postData.materialName === '' || postData.model === '' || postData.providerName === '') {
      return null;
    }
    return postData;
  };

  this.checkValid = function (canUpdate) {
    var fields = requiredFields;

    if (!CheckVaildUtil.checkNullValue(fields, this, fn.getErrorMsg)) {
      return false;
    }

    if (canUpdate) {
      if (!CheckVaildUtil.checkNullValue(feedFields, this, fn.getErrorMsg)) {
        return false;
      }
    }

    if (this.mode === 'feed') {
      if (!CheckVaildUtil.checkNullValue(['feedCount'], this, fn.getErrorMsg)) {
        return false;
      }
    }
    return true;
  };

  this.getObject = function (canUpdate) {
    var postData = {};

    var requiredFields = ['materialName', 'model', 'providerName', 'color', 'colorDescription'];
    this.setPostData(postData, requiredFields, true, true);

    var materialDept = this.materialDept;
    if (materialDept !== '') {
      postData.materialDeptId = materialDept.materialDeptId;
    }

    if (canUpdate) {
      var fields = ['material', 'isEquals', 'defaultWarehouse'];
      fields.pushArray(feedFields);
      if (this.materialType === '镜片') {
        fields.push('transmittance');
      }
      this.setPostData(postData, fields);
      postData.status = '使用';
    }

    var mode = this.mode;
    switch (mode) {
      case 'feed':
        this.setPostData(postData, ['feedCount', 'detailId']);
        break;
      case 'nonFeed':
        postData.company = this.company;
        postData.orderId = this.orderNo;
        postData.oldOrderInfoDetail = JSON.stringify(this.oldFeed);
        postData.materialType = this.materialType;
        break;
    }

    return postData;
  };

  this.getPrePurchaseObj = function (feed, purchaseCount) {
    var postData = {
      articleNo: feed.articleNo,
      orderId: feed.orderNo,
      companyOrderId: feed.companyOrderId,
      factoryNo: this.company,
      purchaseCount: purchaseCount,
      price: this.unitPrice
    };

    var fields = _.cloneDeep(changeFields);
    fields.pushArray(['defaultWarehouse', 'materialType', 'unit', 'material', 'procurementCycles', 'colorDescription']);
    this.setPostData(postData, fields, true, true);

    if (this.materialType === '镜片') {
      postData.transmittance = this.transmittance;
    }

    postData.oldOrderInfoDetail = JSON.stringify(this.oldFeed);
    return postData;
  };

  var params = [material, feed, mode];
  params = _.filter(params, function (param) {
    return param !== undefined;
  });
  if (params.length < 3) {
    material = null;
    feed = params[0];
    mode = params[1];
  }
  fn.initData(material, feed, mode);
};
